/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package service.impl;

import eao.impl.AbstractFacade;
import eao.impl.CustomerFacade;
import javax.ejb.EJB;
import javax.ejb.Stateful;
import javax.ejb.Stateless;
import model.entity.Customer;
import eao.EaoLocal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.entity.Customerdemand;
import service.ICustomerdemandManager;
import service.InterfaceServiceLocal;

/**
 *
 * @author dingnangen
 */
@Stateless
public class CustomerdemandManager extends AbstractService<Customerdemand> implements ICustomerdemandManager<Customerdemand> {

    @EJB(beanName = "CustomerdemandFacade")
    eao.EaoLocal<Customerdemand> customerdemandFacade;

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @Override
    protected EaoLocal getEao() {
        return customerdemandFacade;
    }

    @Override
    public String customerdemandCommit(String cdid, String date) {
        Customerdemand cd = find(Integer.parseInt(cdid));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
        Date _date = null;

        try {
            _date = simpleDateFormat.parse(date);
        } catch (ParseException ex) {
            _date = new Date();
            Logger.getLogger(CustomerdemandManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            cd.setArrivetime(_date);
            cd.setStatus(1);
            customerdemandFacade.edit(cd);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "failed";
        }
    }

    @Override
    public String copyCD(String startdate, String enddate) {

        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date _startdate = simpleDateFormat.parse(startdate);
            Date _enddate = simpleDateFormat.parse(enddate);
            List<String> p = new ArrayList();
            p.add(simpleDateFormat.format(_startdate));
            p.add(simpleDateFormat.format(_enddate));
            return customerdemandFacade.nativeQuery("{call copyCD(?,?)}", p).toString();

        } catch (ParseException ex) {
            Logger.getLogger(CustomerdemandManager.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }

    }

}
